<?php
	$imgName=$_FILES['image']['name'];	
	$old_imgName=$_POST["old_image"];
	if($imgName!="")
	{
		$imgType=$_FILES['image']['type'];
		if(strpos($imgType,"image/")===false)
		{
			echo "<script>alert('Vui lòng chọn file có định dạng hình ảnh!');history.go(-1)</script>";
			return;
		}
	
		$imgSize=$_FILES['image']['size'];	
		if($imgSize>1024*1024)
		{
			echo "<script>alert('Vui lòng chọn file có kích thước nhỏ hơn 1MB!');history.go(-1)</script>";
			return;
		}
		
		$imgName=str_replace(" ","",$imgName);
		$imgName=str_replace("-","",$imgName);
		$imgName=date("y_m_d_h_m_s").(strlen($imgName)>4?substr($imgName,strlen($imgName)-4):"");
		if(!move_uploaded_file($_FILES['image']['tmp_name'],"images/news/".$imgName))
		{
			echo "<script>alert('Lỗi trong quá trình upload file. Vui lòng thử lại!');history.go(-1)</script>";
			return;
		}
		chmod("images/news/".$imgName,0644);
		thumbnail($imgName,$imgType,136,125,"images/news/","images/news/thumbnail/");	
	}
	
	$sql="UPDATE tbl_news SET
	`title`='$_POST[title]',
	".($imgName==""?"":"image='$imgName',")."
	detail='$_POST[detail]'				 
	WHERE id='$_POST[guid]'";
	$res=mysql_query($sql);
			
	if(!$res)
	{
		echo "<script>history.go(-1)</script>";
	}
	else 
	{
		if($imgName!="")
		{
			@unlink("images/news/$old_imgName");
			@unlink("images/news/thumbnail/$old_imgName");
		}
		echo "<script>window.location='?zone=news'</script>";
	}
?>

